package com.yanqu.road.server.cmd.activity.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWar;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@Cmd(code = Protocol.S_UNION_WAR_MATCH_VIDEO_LIST,desc = "精彩对阵")
public class UnionWarMatchVideoListCmd extends AbstractCommand {

    private  int activityId;
    private int phase;
    private int session;
    private int endSession;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionWarActivityServerProto.MatchVideoListReqMsg reqMsg = UnionWarActivityServerProto.MatchVideoListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        phase = reqMsg.getPhase();
        session = reqMsg.getSession();
        if(reqMsg.hasEndSession()){
            endSession = reqMsg.getEndSession();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
        if(activityInfo == null){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        UnionWar unionWar = UnionPhaseWarMgr.getUnionWarMap().get(activityId);
        if(unionWar == null){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        UnionWarActivityServerProto.MatchVideoListRespMsg.Builder respMsg = UnionWarActivityServerProto.MatchVideoListRespMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setPhase(phase);
        respMsg.setSession(session);

        //分组
        int groupId = GameUnionActivityGroupMgr.getViewDataGroupId(activityId, player.getUserId());
        if(endSession > 0){
            for(int i = endSession; i >= session; i--){
                List<UnionWarActivityServerProto.MatchVideoBattleTemp> battleTempList = unionWar.getMatchVideoBattleTempList(phase, i, groupId);
                respMsg.addAllUnionDataList(battleTempList);
            }
        }else {
            List<UnionWarActivityServerProto.MatchVideoBattleTemp> battleTempList = unionWar.getMatchVideoBattleTempList(phase, session, groupId);
            respMsg.addAllUnionDataList(battleTempList);
        }
        player.sendPacket(Protocol.U_UNION_WAR_MATCH_VIDEO_LIST, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            UnionWarActivityServerProto.MatchVideoListRespMsg.Builder respMsg = UnionWarActivityServerProto.MatchVideoListRespMsg.newBuilder();
            respMsg.setActivityId(activityId);
            respMsg.setPhase(phase);
            respMsg.setSession(session);
            player.sendPacket(Protocol.U_UNION_WAR_MATCH_VIDEO_LIST, respMsg);
        }
    }
}
